package com.cat.dataStructure;

import java.util.PriorityQueue;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/k-th-nearest-obstacle-queries/
 * @create 2025/10/6 12:40
 * @since JDK17
 */

public class Solution97 {
    public int[] resultsArray(int[][] queries, int k) {
        int n = queries.length, i = 0;
        int[] ans = new int[n];
        PriorityQueue<Integer> heap = new PriorityQueue<>((a ,b) -> b - a);
        for (var x : queries) {
            int p = Math.abs(x[0]) + Math.abs(x[1]);
            heap.add(p);
            if (heap.size() > k) {
                heap.poll();
            }
            if (heap.size() < k) {
                ans[i++] = -1;
            } else {
                ans[i++] = heap.peek();
            }
        }

        return ans;
    }
}
